import axios from "axios";
import {handleError} from "@/plugins/networkHadleErr";
import store from "@/store";

export let baseURL = process.env.NODE_ENV === 'development' ? process.env.VUE_APP_DEV_API : process.env.VUE_APP_PROD_API;
export let instance = axios.create({
    //不要忘记了下标取值的下一步
    baseURL: baseURL,
    headers: {
        "Content-Type": "application/json"
    },
    responseType: "json",
    // withCredentials: true,
    // 向服务器发送请求时携带cookie,前端同意了还要后端同意 后端在cors插件配置中 加上 credentials : true 部署到服务器之后会遇到问题 暂时取消这种 采用 interceptors.request
    timeout: 8000,
})

instance.interceptors.request.use((config) => {
    let token = store.getters['GET_TOKEN']
    //这里判断本地存储中token是否存在，实现token的自动添加
    if (token) {
        config.headers.token = token
    }
    return config
}, handleError)
instance.interceptors.response.use(res => {
    return res.data.results
}, handleError)

